const {createServer} = require('http')
const {Server} = require('socket.io') 
const httpServer = createServer()
const chatRoom = {}


const IO = new Server(httpServer, {
  path: '/socket'
})
// // 监听链接这个socket服务链接成功的事件
// // 只要你链接成功了，就会给你生成一个socket实例(喇叭)
IO.on('connection', socket => {
  console.log('123123')
  const userId = socket.handshake.query.id
  chatRoom[userId] = socket.id
  socket.on('sendMsg', (info) => {
        console.log('有人发送消息了', info)
        const {toId} = info
        const toSocketId = chatRoom[toId]
        socket.to(toSocketId).emit('receiveMsg', info)
      })
      socket.on('ping', () => {
        socket.emit('pang')
      })

})
httpServer.listen(3000, () => {
  console.log('服务启动成功2')
})

